﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PRE.catalog
{
    public partial class FrmBenhnhan : PRE.catalog.FrmBase
    {
        DAL.BenhnhanDAL _dal = new DAL.BenhnhanDAL();
        BLL.BenhnhanBLL _bll = new BLL.BenhnhanBLL();

        public FrmBenhnhan()
        {
            InitializeComponent();
        }


        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {

            ReadOnlyControl(false);
            ClearDataBindings();
            ChangeStatus(false);

            string _ma = _bll.CreateID();
            txtMaBN.Text = _ma;

            txtHoten.ResetText();
            txtDiachi.ResetText();
            txtDienthoai.ResetText();
            dteNamsinh.EditValue = _bll.GetDate();
            cbomadt.ResetText();
            txtHoten.Focus();



            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            string id = txtMaBN.Text;

            DialogResult result;
            var ok = _dal.Delete(id);
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            ChangeStatus(false);
            txtHoten.Focus();
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }

            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
            var name = Proper(txtHoten.Text);
            var address = Proper(txtDiachi.Text);
            if (_bll.check_SDT(txtDienthoai.Text.Trim()) == false || txtHoten.Text.Trim()=="")
            {
                if (txtHoten.Text.Trim() == "")
                    MessageBox.Show("Họ tên không được để trống");
                else
                    MessageBox.Show("Số điện thoại nhập không đúng");
            }
            else
            {
                if (IsAdd)
                {
                    var o = new DAL.Entities.benhnhan()
                    {
                        ma_bn = txtMaBN.Text,
                        hoten_bn = name,
                        namsinh_bn = Convert.ToDateTime(dteNamsinh.EditValue),
                        gioitinh_bn = Convert.ToBoolean(radioGioitinh.EditValue),
                        diachi_bn = address,
                        sdt_bn = txtDienthoai.Text,

                        madt = (cbomadt.EditValue + "").Trim()
                    };

                    var oki = _dal.Insert(o);
                    if (oki)
                    {
                        MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        PerformRefresh();
                        ChangeStatus(false);
                        PerformAdd();
                    }
                    else
                    {
                        MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        ChangeStatus(false);
                        ReadOnlyControl(false);
                    }

                }

                if (IsEdit)
                {
                    //var _mabn = grvMain.GetFocusedRowCellValue("MaBN") + "";
                    string _mabn = txtMaBN.Text;
                    var o = new DAL.Entities.benhnhan()
                    {
                        ma_bn = _mabn.Trim(),
                        hoten_bn = name,
                        namsinh_bn = Convert.ToDateTime(dteNamsinh.EditValue),
                        gioitinh_bn = Convert.ToBoolean(radioGioitinh.EditValue),
                        diachi_bn = address,
                        sdt_bn = txtDienthoai.Text,
                        madt = cbomadt.EditValue + ""

                    };

                    var oki = _dal.Update(o);
                    if (oki)
                    {
                        MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        PerformRefresh();
                        ChangeStatus();
                    }
                    else
                    {
                        MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        ChangeStatus(false);
                        ReadOnlyControl(false);
                    }
                }
            }

            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {

            txtDiachi.Text = null;
            txtDienthoai.Text = null;
            txtHoten.Text = null;
            dteNamsinh.EditValue = DateTime.Now;
            radioGioitinh.EditValue = true;
            cbomadt.Text = null;
            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txtMaBN.DataBindings.Clear();
            txtHoten.DataBindings.Clear();
            dteNamsinh.DataBindings.Clear();
            radioGioitinh.DataBindings.Clear();
            txtDiachi.DataBindings.Clear();
            txtDienthoai.DataBindings.Clear();
            cbomadt.DataBindings.Clear();
           

            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {
            txtMaBN.DataBindings.Add("EditValue", _dtb, ".Ma_BN");
            txtHoten.DataBindings.Add("EditValue", _dtb, ".HoTen_BN");
            dteNamsinh.DataBindings.Add("EditValue", _dtb, ".Namsinh_BN");
            radioGioitinh.DataBindings.Add("EditValue", _dtb, ".Gioitinh_BN");
            txtDiachi.DataBindings.Add("EditValue", _dtb, ".Diachi_BN");
            txtDienthoai.DataBindings.Add("EditValue", _dtb, ".sdt_BN");
            cbomadt.DataBindings.Add("EditValue", _dtb, ".MADT");
           

            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            //txtMaHV.Properties.ReadOnly = isReadOnly;
            txtHoten.Properties.ReadOnly = isReadOnly;
            dteNamsinh.Properties.ReadOnly = isReadOnly;
            radioGioitinh.Properties.ReadOnly = isReadOnly;
            txtDiachi.Properties.ReadOnly = isReadOnly;
            txtDienthoai.Properties.ReadOnly = isReadOnly;
          
            cbomadt.Properties.ReadOnly = isReadOnly;

            grcMain.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            base.PerformSearch();
        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            //DataTable tmp = _dal3.Select_Current(DateTime.Now);
            //string _makhoa = tmp.Rows[0]["Makhoa"] + "";
            _dtb = _dal.Select();
            

            if (_dtb != null)
            {
                grcMain.DataSource = _dtb;
                label1.Text = "Tổng số bệnh nhân hiện tại  có : " + Convert.ToString(_dtb.Rows.Count) + " bệnh nhân";
            }

            base.LoadData();
        }
        #endregion

        private void FrmBenhnhan_Load(object sender, EventArgs e)
        {
            DataTable _dtb2 = _dal.Select_madt();
            cbomadt.Properties.DataSource = _dtb2;

            PerformRefresh();
        }

        private void txtDienthoai_EditValueChanged(object sender, EventArgs e)
        {
            string a=txtDienthoai.Text.Trim();
            if (IsAdd)
            {
                if (_bll.check_SDT(a) == true)
                {
                    picSDTok.Visible = true;
                    picSDTnotOK.Visible = false;
                }
                else
                {
                    picSDTnotOK.Visible = true;
                    picSDTok.Visible = false;
                }
            }
        }


    }
}
